﻿/**
 *	(C) 2014 Yuanjj
 *
 *	define data base protocal thread class
 *
 *	histroy
 * 		2014-08-07	yuanjj	- create the file
 */

#ifndef _DB_THREAD_H_
#define _DB_THREAD_H_

#include "../base_thread.h"
#include "typedef.h"
#include "errorcode.h"
#include "routine/interface/db/db.h"
#include "../../protocal/protocal.h"
#include "../../protocal/db_protocal.h"

class db_thread : public base_thread
{
public:
    db_thread();
    ~db_thread();

protected:
    // deal protocal recv from the db tunnel
    pem_error_t		deal_protocal( pem_s8_t* pdata, pem_u32_t size );

protected:
    /**********************************************************
     *	user data
     **********************************************************/
    // user login
    pem_error_t		_user_login( pem_s8_t* pdata, pem_u32_t size );
    pem_error_t		__user_login( pem_s8_t* pdata, pem_u32_t size );

    /**********************************************************
     *	host data
     **********************************************************/
    // load host
    pem_error_t		_load_host( pem_s8_t* pdata, pem_u32_t size );
    pem_error_t		__load_host( cls_res& sqlres );

    /**********************************************************
     *	tunnel data
     **********************************************************/
    // load tunnel type form data base
    pem_error_t		_load_tunnel_type( pem_s8_t* pdata, pem_u32_t size );
    pem_error_t 	__load_tunnel_type( cls_res& sqlres );

     // load tunnel form data base
    pem_error_t		_load_tunnel( pem_s8_t* pdata, pem_u32_t size );
    pem_error_t 	__load_tunnel( cls_res& sqlres );

    /** update tunnel info to data base */
    pem_error_t		_update_tunnel( pem_s8_t* pdata, pem_u32_t size );

    /** delete tunnel info from data base */
    pem_error_t		_delete_tunnel( pem_s8_t* pdata, pem_u32_t size );

    /**********************************************************
     *	device data
     **********************************************************/
    // load device type from data base
    pem_error_t		_load_device_type( pem_s8_t* pdata, pem_u32_t size );
    pem_error_t 	__load_device_type( cls_res& sqlres );

    // load device from data base
    pem_error_t		_load_device( pem_s8_t* pdata, pem_u32_t size );
    pem_error_t 	__load_device( cls_res& sqlres );

    // load protocal from data base
    pem_error_t		_load_protocal( pem_s8_t* pdata, pem_u32_t size );
    pem_error_t 	__load_protocal( cls_res& sqlres );

    // update device info to data base
    pem_error_t		_update_device( pem_s8_t* pdata, pem_u32_t size );
    // set up device
    pem_error_t 	__setup_device( db_device_update* device );

    /**********************************************************
     *	alarm data
     **********************************************************/
    // load alarm from data base
    pem_error_t		_load_alarm( pem_s8_t* pdata, pem_u32_t size );
    pem_error_t 	__load_alarm( cls_res& sqlres );

    /**********************************************************
     *	event data
     **********************************************************/
    // load event type from data base
    pem_error_t		_load_event_type( pem_s8_t* pdata, pem_u32_t size );
    pem_error_t 	__load_event_type( cls_res& sqlres );

    // load event from data base
    pem_error_t		_load_event( pem_s8_t* pdata, pem_u32_t size );
    pem_error_t 	__load_event( cls_res& sqlres );

    /**********************************************************
     *	scene data
     **********************************************************/
    // load scene from data base
    pem_error_t		_load_scene( pem_s8_t* pdata, pem_u32_t size );
    pem_error_t 	__load_scene( cls_res& sqlres );

    // save scene into data base
    pem_error_t		_save_scene( pem_s8_t* pdata, pem_u32_t size );
    // update scene to ui
    pem_error_t 	__update_scene( head_in& headdb, db_scene_save* save );

    // load frame from data base
    pem_error_t		_load_frame( pem_s8_t* pdata, pem_u32_t size );
    pem_error_t 	__load_frame( cls_res& sqlres );
};

#endif // _DB_THREAD_H_
